<!DOCTYPE html>
<!--
Copyright 2015 Mozilla Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SWFM recorder</title>
  <style>
    body {
      font-family: Sans-Serif;
    }
  </style>
  <script>
    function parseQueryString(qs) {
      if (!qs)
        return {};

      if (qs.charAt(0) == '?')
        qs = qs.slice(1);

      var values = qs.split('&');
      var obj = {};
      for (var i = 0; i < values.length; i++) {
        var kv = values[i].split('=');
        var key = kv[0], value = kv[1];
        obj[decodeURIComponent(key)] = decodeURIComponent(value);
      }

      return obj;
    }

    var params = parseQueryString(document.location.search);
    var iframeViewerPath = '../../iframe/viewer.html';

    function startSwf(url) {
      var viewer = document.getElementById('viewer');
      viewer.textContent = '';
      var iframe = document.createElement('iframe');
      iframe.setAttribute('frameborder', '0');
      iframe.id = 'player';
      iframe.src = iframeViewerPath;
      iframe.width = (params.width | 0) || 640;
      iframe.height = (params.heigth | 0) || 480;
      iframe.onload = function () {
        iframe.contentWindow.postMessage({
          type: 'pluginParams',
          flashParams: {
            recordingLimit: (params.limit | 0) || 1000000,
            url: new URL(url, document.location).href
          }
        }, '*');
      };
      viewer.appendChild(iframe);
    }

    function stopRecording() {
      var recorder = document.getElementById('player').contentWindow.gfxWindow.easelHost.recorder;
      var recording = recorder.getRecording();

      var recordingHref = document.getElementById('recording');
      recordingHref.href = URL.createObjectURL(recording);
      recordingHref.removeAttribute('hidden');
    }

    function restart() {
      startSwf(params.swf);
    }
  </script>
</head>
<body>
  <div>
    <button onclick="restart()">Restart</button>
    <button onclick="stopRecording()">Stop recording</button>
    <a id="recording" href="" download="test.swfm" hidden>Download</a>
  </div>
  <div id="viewer"></div>
  <script>
    restart();
  </script>
</body>
</html>
